package com.meet.service;

import com.meet.dao.ConnectionByWMDao;
import com.meet.dao.MeetingDao;
import com.meet.dao.WorkersDao;
import com.meet.dao.ConnectionByWMDaoImpl;
import com.meet.dao.MeetingDaoImpl;
import com.meet.dao.WorkersDaoImpl;
import com.meet.entity.ConnectionByWM;
import com.meet.entity.PageBean;
import com.meet.service.ConnectionService;
import com.meet.vo.ConnectionVo;
import com.meet.vo.WorkersVo;

import java.sql.SQLException;
import java.util.List;

public class ConnectionServiceImpl implements ConnectionService {

    private ConnectionByWMDao wmDao = new ConnectionByWMDaoImpl();

    /**
     * 删除连接
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int dropConnection(Integer id) throws SQLException {
        return wmDao.dropConnection(id);
    }

    /**
     * 获取所有数据的总数
     *
     * @return
     * @throws SQLException
     */
    @Override
    public int totalPage() throws SQLException {
        return wmDao.totalPage();
    }

    /**
     * 添加数据
     *
     * @param connectionByWM
     * @return
     */
    @Override
    public int addConnection(ConnectionByWM connectionByWM) throws SQLException {
        return wmDao.addConnection(connectionByWM);
    }

    /**
     * 根据id获取连接
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public ConnectionByWM getWM(Integer id) throws SQLException {
        return wmDao.getWM(id);
    }

    /**
     * 查找所有连接
     *
     * @param page
     * @param num
     * @return
     * @throws SQLException
     */
    @Override
    public PageBean<ConnectionVo> getAllWM(int page, int num,int id) throws SQLException {

        if(page <=0) {
           page = 1;
        }

        //1.创建空的PageBean对象
        PageBean<ConnectionVo> pb = new PageBean<>();
        //2.设置参数
        pb.setCurrentPage(page);
        pb.setRows(num);

        //3.调用dao查询总记录数
        int totalCount = this.totalPage();
        pb.setTotalCount(totalCount);

        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (page - 1) * num;
        List<ConnectionVo> list = wmDao.getAllWM(start,num,id);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % num)  == 0 ? totalCount/num: (totalCount/num) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }

    /**
     * 更新连接
     *
     * @param wm
     * @return
     * @throws SQLException
     */
    @Override
    public int updateWM(ConnectionByWM wm) throws SQLException {
        return wmDao.updateWM(wm);
    }

    /**
     * 修改是否确定
     *
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int yes(Integer id) throws SQLException {
        return wmDao.yes(id);
    }
}
